package com.jmpt.fangbohui.ui.holder;

import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;

import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.ui.ViewPictureActivity;
import com.jmpt.fangbohui.ui.base.BaseViewHolder;
import com.jmpt.fangbohui.utils.UIUtils;
import com.jmpt.fangbohui.view.HorizontalListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class HorizontalImageListHolder extends BaseViewHolder<String[][]> {
	
	@ViewInject(R.id.horizontallistview1)
	private HorizontalListView mHlvPicList;
	
	private HorizontalListViewAdapter mAdapter;
	
	private String[] mPicUrls;
	private String[] mPicTitles;

	public HorizontalImageListHolder(Context context) {
		super(context);
	}

	@Override
	protected View initView() {
		View view = View.inflate(UIUtils.getContext(), R.layout.horizontal_listview_layout, null);
		
		ViewUtils.inject(this,view);
		
		
		mHlvPicList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				startViewPictureActivity(position);
			}
		});
		
		return view;
	}
	
	@Override
	protected void refreshView(String[][] data) {
		
		try {
			mPicUrls = data[0];
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			mPicTitles = data[1];
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(mAdapter == null){
			mAdapter=new HorizontalListViewAdapter();
			mHlvPicList.setAdapter(mAdapter);
		}
		mAdapter.notifyDataSetChanged();
	}

	class HorizontalListViewAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			if(mPicUrls != null){
				return mPicUrls.length;
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			String[] item = new String[2];
			try {
				item[0] = mPicUrls[position];
				item[1] = mPicTitles[position];
			} catch (Exception e) {
				e.printStackTrace();
			}
			return item;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			HorizontalImageItemHolder holder;
			if(convertView == null){
				holder = new HorizontalImageItemHolder(mContext);
				convertView = holder.getRootView();
			}else{
				holder = (HorizontalImageItemHolder) convertView.getTag();
			}
			
			holder.setData((String[])getItem(position), position);
			
			return convertView;
		}
		
	}
	
	protected void startViewPictureActivity(int position) {
		Intent intent = new Intent(UIUtils.getContext(), ViewPictureActivity.class);
		intent.putExtra(ViewPictureActivity.START_INDEX, position);
		intent.putExtra(ViewPictureActivity.PICTURE_ARRAY, mPicUrls);
		intent.putExtra(ViewPictureActivity.TITLE_ARRAY, mPicTitles);
		mContext.startActivity(intent);
	}
	
}
